package easytalk.views.custom;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

import common.EasyChatEnvironment;

import connection.IServerConnection;
import connection.ServerConnectionConfig;
import easytalk.connectivity.facebook.FacebookConnection;
import easytalk.connectivity.googletalk.GoogleTalkConnection;
import easytalk.connectivity.msn.MsnConnection;
import easytalk.connectivity.offline.OfflineServer;

public class ConnectionProgress implements IWorkbenchWindowActionDelegate {

	private IWorkbenchWindow window;
	private ServerConnectionConfig config;
	private boolean error = false;
	private String errorMessage;

	public ConnectionProgress(ServerConnectionConfig config) {
		super();
		this.config = config;
	}

	@Override
	public void dispose() {

	}

	public String getErrorMessage() {
		return errorMessage;
	}

	@Override
	public void init(IWorkbenchWindow window) {
		this.window = window;

	}

	public boolean isError() {
		return error;
	}

	@Override
	public void run(IAction action) {
		ProgressMonitorDialog dialog = new ProgressMonitorDialog(window
				.getShell());

		try {

			dialog.run(true, false, new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor) {
					monitor.beginTask(Messages.getString("ConnectionProgress.0"), 100); //$NON-NLS-1$
					IServerConnection connection = null;
					try {
						
						switch (config.getServerType()) {
						case GOOGLE_TALK:
							connection = new GoogleTalkConnection();
							break;

						case MSN:
							connection = new MsnConnection();
							break;

						case OFFILNE:
							connection = new OfflineServer();
							break;
							
						case FACEBOOK:
							connection = new FacebookConnection();
							break;
						default:
							return;
						}
						
						connection.addProgressListener(monitor);
						
						if (connection.login(config.getUserName(), config
								.getPassword())) {
							EasyChatEnvironment.getInstance()
									.addServerConnection(connection);
							
						}

					} catch (Exception e) {
						e.printStackTrace();
						setError(e.getMessage());
					} finally {
						monitor.done();
						connection.removeProgressListeners();
					}
				}
			});

		} catch (InvocationTargetException e) {
			setError(e.getMessage());
		} catch (InterruptedException e) {
			setError(e.getMessage());
		}

	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub

	}

	public void setError(boolean error) {
		this.error = error;
	}

	protected void setError(String message) {
		this.error = true;
		if (message != null)
			this.setErrorMessage(message);
		else
			setErrorMessage(Messages.getString("ConnectionProgress.1")); //$NON-NLS-1$

	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	private void showMessage(String message) {
		MessageDialog.openInformation(window.getShell(), Messages.getString("ConnectionProgress.2"), message); //$NON-NLS-1$
	}

}
